package com.irishud.hudnavi;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class x3obdDemoActivity extends Activity implements ViewSwitcher.ViewFactory {
    ImageSwitcher gallery;

    List<Integer> list = new ArrayList<Integer>();
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_x3obd_demo);
        gallery = (ImageSwitcher)findViewById(R.id.img_switch);

        list.add( R.drawable.p1);
        list.add( R.drawable.p2);
        list.add( R.drawable.p3);
        list.add( R.drawable.p4);
        list.add( R.drawable.p5);
        gallery.setFactory(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void handleX3ObdEvent(x3obdDemoEvent event){
        if(event.getEvent() == x3obdDemoEvent.GoNext){
            gallery.setImageResource(list.get(index++ % list.size()));
        }
        if(event.getEvent() == x3obdDemoEvent.GoPrev){
            if(index-- < 0){
                index = 0;
            }
            gallery.setImageResource(list.get(index--));
        }

        if(event.getEvent() == x3obdDemoEvent.Close){
            finish();
        }
    }

    @Override
    public View makeView() {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(list.get(index));
        return imageView;
    }
}
